
function _R.Player:ResetData( )
	tmysql.query("DELETE FROM players WHERE steamid='" .. self:SteamID() .. "'", SQLHandle )
end

function _R.Player:SetData( )
	if self.Stats["ID"] then
		local oldtbl = {self, self:Nick(), self.Stats["Kills"], self.Stats["Deaths"], self.Stats["Headshots"], self.Stats["Points"], self.Stats["Time"]}
		self.Stats["Time"] = self.Stats["Time"] + self:TimeConnected()
		tmysql.query("UPDATE players SET nick='" .. tmysql.escape(self:Nick()) .. "', kills='" .. self.Stats["Kills"] .. "', deaths='" .. self.Stats["Deaths"] .. "', headshots='" .. self.Stats["Headshots"] .. "', points='" .. self.Stats["Points"] .. "', time='" .. self.Stats["Time"] .. "' WHERE steamid='" .. self:SteamID() .. "'", SQLHandle)
		local newtbl = {self, self:Nick(), self.Stats["Kills"], self.Stats["Deaths"], self.Stats["Headshots"], self.Stats["Points"], self.Stats["Time"]}
		hook.Call("SetData", GAMEMODE, oldtbl, newtbl)
	end
end

function _R.Player:GetData( )
	self.Stats = {}
	tmysql.query("SELECT * FROM players WHERE steamid='" .. self:SteamID() .. "'", function( Args )
		if Args and Args[1] then
			self.Stats["ID"] = tonumber(Args[1][1])
			self.Stats["Kills"] = tonumber(Args[1][4])
			self.Stats["Deaths"] = tonumber(Args[1][5])
			self.Stats["Headshots"] = tonumber(Args[1][6])
			self.Stats["Points"] = tonumber(Args[1][7])
			self.Stats["Time"] = tonumber(Args[1][8])
			self.Stats["Victims"] = tonumber(Args[1][9])
			self.Stats["Attackers"] = tonumber(Args[1][10])
			self.Stats["Weapons"] = tostring(Args[1][11])
			self:SetPosition()
			self:SetWeapons()
			self.Stats["GotHeadShot"] = false
			timer.Simple(3, function()
				for k, v in pairs(player.GetAll()) do
					chat.AddText(v, Color(25, 151, 25), "[gStats] ", Color(249, 247, 237), self:Nick() .. " has joined the server. They are currently ranked " .. self.Stats["Rank"] .. " with " .. self.Stats["Points"] .. " points.")
				end
			end)
		else
			tmysql.query("INSERT INTO players (steamid, nick, kills, deaths, headshots, points, time) VALUES( '" .. tmysql.escape( self:SteamID() ) .. "', '" .. tmysql.escape( self:Nick() ) .. "', '" .. 0 .. "', '" .. 0 .. "', '" .. 0 .. "', '" .. 1000 .. "', '" .. self:TimeConnected() .. "' )" )
			chat.AddText(self, Color(25, 151, 25), "[gStats] ", Color(249, 247, 237), "New account created")
			timer.Simple(3, function()
				tmysql.query("SELECT * FROM players WHERE steamid='" .. self:SteamID() .. "'", function( Args )
					if Args and Args[1] then
						self.Stats["ID"] = tonumber(Args[1][1])
						self.Stats["Kills"] = tonumber(Args[1][4])
						self.Stats["Deaths"] = tonumber(Args[1][5])
						self.Stats["Headshots"] = tonumber(Args[1][6])
						self.Stats["Points"] = tonumber(Args[1][7])
						self.Stats["Time"] = tonumber(Args[1][8])
						self.Stats["Victims"] = tonumber(Args[1][9])
						self.Stats["Attackers"] = tonumber(Args[1][10])
						self.Stats["Weapons"] = tostring(Args[1][11])
						self:SetPosition()
						self:SetWeapons()
						self.Stats["GotHeadShot"] = false
					end
				end)
			end)
		end
	end)
end

function _R.Player:Rank( )
	tmysql.query("SELECT kills, deaths, points FROM players WHERE steamid='" .. self:SteamID() .. "'", function( Args )
		if Args and Args[1] then
			local kills = tonumber(Args[1][1])
			local deaths = tonumber(Args[1][2])
			local points = tonumber(Args[1][3])
			umsg.Start("gStats.Rank", self)
				umsg.String(self.Stats["Rank"] .. ";" .. points .. ";" .. kills .. ";" .. deaths)
			umsg.End()
		end
	end)
end

function _R.Player:Session( )
	umsg.Start("gStats.Session", self)
		umsg.String(self.Stats["Rank"] .. ";" .. self.Stats["Points"] .. ";" .. self:Frags() .. ";" .. self:Deaths())
	umsg.End()
end

Top10 = {}
function _R.Player:Top10( )
	table.Empty(Top10)
	tmysql.query("SELECT nick, points FROM players ORDER BY points DESC LIMIT 10", function( Args )
		if !Args and !Args[1] then
			chat.AddText(self, Color(25, 151, 25), "[gStats] ", Color(249, 247, 237), "Error unable to get top10, contact an admin(mysql query error)")
		end
		umsg.Start("gStats.Clear", self)
		umsg.End()
		for k, v in pairs(Args) do
			umsg.Start("gStats.Top10", self)
				umsg.String(Args[k][1])
				umsg.Float(Args[k][2])
			umsg.End()
		end
	end)
end

function _R.Player:SetPosition()
	tmysql.query("SELECT points, kills, deaths, time FROM players WHERE steamid='" .. self:SteamID() .. "'", function( Arg )
		if Arg and Arg[1] then
			tmysql.query("SELECT COUNT(1) FROM players WHERE points >='" .. Arg[1][1] .. "' AND kills >='" .. Arg[1][2] .. "' AND deaths <='" .. Arg[1][3] .. "' AND time <='" .. Arg[1][4] .. "'", function( Args )
				if Args and Args[1] then
					self.Stats["Rank"] = tonumber(Args[1][1]) or 0
				end
			end)
		end
	end)
end

function _R.Player:SetWeapons()
	local Table = self.Stats["Weapons"]
	local index = string.Explode(";", Table)
	self.Stats["Weapons"] = {}
	for k, v in pairs(index) do
		if v != "" then
			local value = string.Explode(":", v)
			self.Stats["Weapons"][tostring(value[1])] = value[2]
		end
	end
end

function _R.Player:UpdateKills()
	local old = self.Stats["Kills"]
	self.Stats["Kills"] = self.Stats["Kills"] + 1
	local tbl = { self, "Kills", old, self.Stats["Kills"] }
	hook.Call("UpdateData", GAMEMODE, tbl)
end

function _R.Player:UpdateDeaths()
	local old = self.Stats["Deaths"]
	self.Stats["Deaths"] = self.Stats["Deaths"] + 1
	local tbl = { self, "Deaths", old, self.Stats["Deaths"] }
	hook.Call("UpdateData", GAMEMODE, tbl)
end

function _R.Player:UpdateHeadshots()
	local old = self.Stats["Headshots"]
	self.Stats["Headshots"] = self.Stats["Headshots"] + 1
	local tbl = { self, "Headshots", old, self.Stats["Headshots"] }
	hook.Call("UpdateData", GAMEMODE, tbl)
end

function _R.Player:UpdatePoints( arg )
	arg = tonumber(arg)
	local old = self.Stats["Points"]
	if arg < 0 then
		self.Stats["Points"] = self.Stats["Points"] - math.abs(arg)
	else
		self.Stats["Points"] = self.Stats["Points"] + arg
	end
	local tbl = { self, "Points", old, self.Stats["Points"]}
	hook.Call("UpdateData", GAMEMODE, tbl)
end
